package org.baicaix.demo.localconnect {
	import org.baicaix.events.DataEvent;

	import flash.events.EventDispatcher;
	import flash.net.LocalConnection;

	/**
	 * @author dengyang
	 */

	/**
	 * 用于显示调试信息，利用localconnection实现
	 * @author coty 
	 * @author dengyang
	 */
	public class LocalConsoleClient extends EventDispatcher {
		
		public static const EVENT_PRINT : String = "print";
		
		//连接
		private var conn : LocalConnection;
		private var sendConn : LocalConnection;
		public static const CONNECTIONNAME : String = "cotyinfoconn";
		public static const GM_COMMAND_FUNCTION : String = "gmCommand";

		public function LocalConsoleClient() {
			initConn();
		}

		/**
		 * 设置连接
		 */
		private function initConn() : void {
			conn = new LocalConnection();
			conn.client = this;
			
			sendConn = new LocalConnection();
			try{
				conn.connect(CONNECTIONNAME);
				//print('连接成功');
			}catch(error:Error){
				print('连接失败');
			}
		}

		/**
		 * 打印信息
		 */
		public function print(value : String) : void {
			this.dispatchEvent(new DataEvent(EVENT_PRINT, value));
		}

		/**
		 * 想服务器端发送信息
		 */
		public function send(cmd : String) : void {
			sendConn.send(CONNECTIONNAME+"1", GM_COMMAND_FUNCTION, cmd);
		}
	}
}

